package com.house.controller;

import com.alibaba.fastjson.JSONObject;
import com.house.entity.Accusation;
import com.house.entity.House;
import com.house.entity.UserInf;
import com.house.entity.UserLogin;
import com.house.service.AccusationService;
import com.house.service.AdminService;
import com.house.service.HouseService;
import com.house.service.UserService;
import com.house.util.PageRequest;
import com.house.util.PageResult;
import com.house.vo.JsonVo;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping(value = "/admin/")
public class AdminController {

    @Autowired
    private UserService userService;
    @Autowired
    private HouseService houseService;
    @Autowired
    private AccusationService accusationService;
    @Autowired
    private AdminService adminService;

    @GetMapping("/api/getmenu")
    @ResponseBody
    public JSONObject getmenu(Authentication auth) throws IOException {
        return (JSONObject)JSONObject.parse(adminService.getmenu(auth));
    }

    @RequestMapping("/indexpage")
    public ModelAndView index(HttpSession session){

        ModelAndView modelAndView=new ModelAndView();
        if (session.getAttribute("userInf")!=null) {
            UserLogin userLogin = (UserLogin)session.getAttribute("userLogin");
            modelAndView.addObject("userlogin",userLogin);
        }
        modelAndView.setViewName("admin/index");
        return modelAndView;
    }

    @RequestMapping("/welcome")
    public ModelAndView welcome(){
        ModelAndView modelAndView=new ModelAndView();
        List<UserInf> userInfList=userService.getAll();
        List<House> houseList=houseService.findAll();
        List<House> willHouse=houseService.findAllByStatus(0);

        modelAndView.addObject("userInfList",userInfList);
        modelAndView.addObject("houseList",houseList);
        modelAndView.addObject("willHouse",willHouse);

        modelAndView.setViewName("admin/page/welcome");
        return modelAndView;
    }

    @RequestMapping("/house/list")
    public ModelAndView list(){
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("admin/house/list");
        return modelAndView;
    }



    @RequestMapping("/api/houselist")
    @ResponseBody
    public JsonVo allHouse(){
        List<House>houseList=houseService.findAll();
        JsonVo jsonVo=new JsonVo();

        jsonVo.setData(houseList);
        jsonVo.setCode(0);

        return jsonVo;
    }

    @RequestMapping("/house/willlist")
    public ModelAndView willlist(){
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("admin/house/will/list");
        return modelAndView;
    }

    @RequestMapping("/api/willlist")
    @ResponseBody
    public PageResult willlist(@RequestParam("page")String pageNum, @RequestParam("limit")String pageSize){

        return houseService.getAllUnRealse(new PageRequest(Integer.parseInt(pageNum),Integer.parseInt(pageSize)));
    }


    @RequestMapping("/user/userlist")
    public ModelAndView userlistpage(){
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("admin/user/list");
        return modelAndView;
    }

    @RequestMapping("/api/userlist")
    @ResponseBody
    public PageResult userlist(@RequestParam("page")String pageNum, @RequestParam("limit")String pageSize){

        return userService.getAll(new PageRequest(Integer.parseInt(pageNum),Integer.parseInt(pageSize)));
    }

    @RequestMapping("/user/cancellist")
    public ModelAndView cancellistpage(){
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("admin/user/cancel/list");
        return modelAndView;
    }

    @RequestMapping("/api/cancellist")
    @ResponseBody
    public JsonVo cancellist(){
        List<UserLogin> userList=userService.findAllByStatus(2);
        JsonVo jsonVo=new JsonVo();

        jsonVo.setData(userList);
        jsonVo.setCode(0);

        return jsonVo;
    }

    @RequestMapping("/api/house/agree")
    @ResponseBody
    public JsonVo agree(@RequestParam("id")int id,@RequestParam("status")int status){
        
        JsonVo jsonVo=new JsonVo();
        houseService.updateStatusByIdAndStatus(id,status);
        jsonVo.setCode(0);

        return jsonVo;
    }

    @RequestMapping("/api/house/disagree")
    @ResponseBody
    public JsonVo disagree(@RequestParam("id")int id,@RequestParam("status")int status){
        
        JsonVo jsonVo=new JsonVo();
        houseService.updateStatusByIdAndStatus(id,status);
        jsonVo.setCode(0);

        return jsonVo;
    }

    @RequestMapping("/api/house/delete")
    @ResponseBody
    public JsonVo deleteHouse(@RequestParam("id")int id){
        
        JsonVo jsonVo=new JsonVo();
        houseService.deleteById(id);
        jsonVo.setCode(0);

        return jsonVo;
    }

    @RequestMapping("/api/user/agree")
    @ResponseBody
    public JsonVo userAgree(@RequestParam("id")int id,@RequestParam("status")int status){
       
        JsonVo jsonVo=new JsonVo();
        userService.updateByIdAndStatus(id,status);
        jsonVo.setCode(0);

        return jsonVo;
    }

    @RequestMapping("/api/user/delete")
    @ResponseBody
    public JsonVo userdelete(@RequestParam("id")int id){
        
        JsonVo jsonVo=new JsonVo();
        userService.deleteById(id);
        jsonVo.setCode(0);

        return jsonVo;
    }


    @RequestMapping("/accusation/list")
    public ModelAndView accusationListPage(){
        ModelAndView modelAndView=new ModelAndView();


        modelAndView.setViewName("admin/accusation/list");

        return modelAndView;
    }


    @RequestMapping("/api/accusation/list")
    @ResponseBody
    public JsonVo accusationlist(){
        List<Accusation> accusations=accusationService.findAll();
        JsonVo jsonVo=new JsonVo();
        jsonVo.setCode(0);
        jsonVo.setData(accusations);
        return jsonVo;
    }

    @RequestMapping("/api/accusation/delete")
    @ResponseBody
    public JsonVo accusationDelete(@RequestParam("id") int id){


        accusationService.deleteById(id);

        JsonVo jsonVo=new JsonVo();
        jsonVo.setCode(0);
        return jsonVo;
    }
    @RequestMapping("/user/repassword")
    public ModelAndView repasswordPage(){
        ModelAndView modelAndView=new ModelAndView();


        modelAndView.setViewName("admin/user/edit");

        return modelAndView;
    }

    @RequestMapping("/api/user/repassword")
    @ResponseBody
    public JsonVo repassword(@RequestParam("id") int id,@RequestParam("repassword")String password){
        
        userService.updatePasswordById(id,password);
        JsonVo jsonVo=new JsonVo();
        jsonVo.setCode(0);
        return jsonVo;
    }
}
